const app = getApp()

// 表单验证
import WxValidate from '../../utils/WxValidate.js'

// 引入请求api
import {
  GetSystemData,
  Register
} from "../../config/api.js"

// 引入请求函数
import _util from "../../utils/util.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: app.globalData.baseImgUrl,
    isRead: false,
    notice: '',
    rules: [],
    showView: true,
    idcard: '',
    // password:'',
    // repeatPassword:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate() //验证规则函数
    this.getUserNotice();
  },

  getUserNotice() {
    let that = this;
    _util.apiRequest(GetSystemData, null, null).then(r => {
      console.log('用户须知', r)
      if (r.data.data) {
        let str = r.data.data.value.replace(/\n/g, "|-&");
        var rules = str.split("|-&");

        that.setData({
          notice: r.data.data,
          rules
        })
      }

    })
  },

  checkboxChange(e) {
    console.log('checkbox发生值为：', e.detail.value)
    let checkValue = e.detail.value
    if (checkValue.length != 0) {
      this.setData({
        isRead: true
      })
    } else {
      this.setData({
        isRead: false
      })
    }
  },

  chose() {
    this.setData({
      showView: false
    })
  },

  close() {
    this.setData({
      showView: true
    })
  },

  onReaded() {
    this.setData({
      isRead: true,
      showView: true
    })
  },

  //报错 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },

  //验证函数
  initValidate() {
    const rules = {
      idcard: {
        required: true,
        idcard: true,
      },
    }
    const messages = {
      idcard: {
        required: '请输入身份证号码',
        //idcard: '请输入正确的身份证号码',
      },
    }
    this.WxValidate = new WxValidate(rules, messages)
  },

  inputId(e) {
    this.setData({
      idcard: e.detail.value
    })
  },

  bindGetUserInfo(e) {
    let that = this;
    console.log('身份', this.data.idcard)
    let openid = wx.getStorageSync('openid')
    console.log('openid:', openid)
    console.log('用户信息', e.detail.userInfo)
    let userinfo = e.detail.userInfo;
    wx.setStorageSync('userInfo', e.detail.userInfo)

    let verifi_params = {
      idcard: this.data.idcard,
    }

    //校验表单
    if (!this.WxValidate.checkForm(verifi_params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }

    if (!this.data.isRead) {
      wx.showModal({
        showCancel: false,
        content: '请先阅读用户协议',
      })
      return;
    }

    let params = {
      idcard: this.data.idcard,
      openid: openid,
      nick_name: userinfo.nickName,
      heard_img: userinfo.avatarUrl,
      vollege_id: app.globalData.vollege_id,
    }

    console.log('提交对象', params)

    _util.apiRequest(Register, params).then(r=>{
        console.log('注册返回：', r)
        if (r.data.code == 0) {
          that.showModal({ msg: r.data.info })
        } else if (r.data.code == 1) {
          if (r.data.data.length != 0) {
            let data_arr = r.data.data;
            let new_data = [];
            wx.setStorageSync('userArr', r.data.data)
            data_arr.forEach(function (item, index) {
              if (item.authorize == 18) {
                new_data.push(item)
              }
            })
            console.log('村民用户：', new_data)
            wx.setStorageSync('member', new_data[0])
            wx.showModal({
              showCancel: false,
              // title: '提示',
              content: r.data.info,
              success(res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '/pages/index/index',
                  })
                }
              }
            })
          }
        }
    })

    // wx.request({
    //   url: app.globalData.baseURL + '/api/WxProgramApi/register',
    //   data: params,
    //   header: {
    //     'content-type': 'application/json' // 默认值
    //   },
    //   success(r) {
    //     console.log('注册返回：', r)
    //     if (r.data.code == 0) {
    //       that.showModal({ msg: r.data.info })
    //     } else if (r.data.code == 1) {
    //       if (r.data.data.length != 0) {
    //         let data_arr = r.data.data;
    //         let new_data = [];
    //         wx.setStorageSync('userArr', r.data.data)
    //         data_arr.forEach(function (item, index) {
    //           if (item.authorize == 18) {
    //             new_data.push(item)
    //           }
    //         })
    //         console.log('村民用户：', new_data)
    //         wx.setStorageSync('member', new_data[0])
    //         wx.showModal({
    //           showCancel: false,
    //           // title: '提示',
    //           content: r.data.info,
    //           success(res) {
    //             if (res.confirm) {
    //               wx.redirectTo({
    //                 url: '/pages/index/index',
    //               })
    //             }
    //           }
    //         })
    //       }
    //     }
    //   }
    // })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})